package com.chen.q2.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class PreparedStatementSqlUtil {
    public static void setPrepareStatementParams(PreparedStatement pst, Object obj) {
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        AtomicInteger index = new AtomicInteger(1);
        Arrays.stream(fields).forEach(field -> {
            int i = index.getAndIncrement();
            field.setAccessible(true);
            try {
                Object value = field.get(obj);

                String type = field.getType().getTypeName();
                if (value == null){
                    pst.setObject(i, value);
                }else if ("java.lang.String".equals(type)) {
                    pst.setString(i, (String) value);
                } else if ("java.lang.Long".equals(type)) {
                    pst.setLong(i, (Long) value);
                } else if ("java.util.Date".equals(type)) {
                    pst.setDate(i, new java.sql.Date(((Date) value).getTime()));
                } else if ("java.lang.Integer".equals(type)) {
                    pst.setInt(i, (Integer) value);
                } else if ("java.math.BigDecimal".equals(type)) {
                    pst.setBigDecimal(i, (BigDecimal) value);
                } else {
                    pst.setObject(i, value);
                }

            } catch (IllegalAccessException | SQLException e) {
                e.printStackTrace();
            }
        });
    }

    public static String generateInsertPreparedStatementSql(Class clazz) {
        String tableName = camelToLine(clazz.getSimpleName());
        String insertSql = "insert into " + tableName;
        StringBuilder colSb = new StringBuilder("(");
        StringBuilder valSb = new StringBuilder("(");
        Field[] fields = clazz.getDeclaredFields();

        Arrays.stream(fields).forEach(field -> {
            String fieldName = field.getName();
            String columnName = camelToLine(fieldName);
            colSb.append(columnName).append(",");
            valSb.append("?,");
        });
        colSb.deleteCharAt(colSb.length() - 1);
        colSb.append(")");
        valSb.deleteCharAt(valSb.length() - 1);
        valSb.append(")");
        insertSql = insertSql + colSb.toString() + " values " + valSb.toString();
        return insertSql;
    }

    private static String camelToLine(String s) {
        String line = "";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i == 0) {
                line += String.valueOf(c).toLowerCase();
            } else {
                if (c >= 'A' && c <= 'Z') {
                    line = line + "_" + String.valueOf(c).toLowerCase();
                } else if (c >= 'a' && c <= 'z') {
                    line += c;
                }
            }
        }
        return line;
    }
}
